package com.ruoyi.project.cms.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.cms.domain.CmsArea;
import com.ruoyi.project.cms.domain.vo.CmsAreaCascaderVo;
import com.ruoyi.project.cms.service.ICmsAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政区划Controller
 * 
 * @author ruoyi
 * @date 2020-10-13
 */
@RestController
@RequestMapping("/area/area")
public class CmsAreaController extends BaseController
{
    @Autowired
    private ICmsAreaService cmsAreaService;

    /**
     * 查询行政区划列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:list')")
    @GetMapping("/list")
    public AjaxResult<List<CmsArea>> list(CmsArea cmsArea)
    {
        cmsArea.setIsDel(Constants.SYS_NO);
        List<CmsArea> list = cmsAreaService.selectCmsAreaList(cmsArea);

        return AjaxResult.success(list);
    }

    /**
     * 行政区划级联
     */
    @GetMapping("/cascader")
    public AjaxResult<List<CmsAreaCascaderVo>> cascader()
    {
        List<CmsAreaCascaderVo> list = cmsAreaService.cmsAreaCascader();
        return AjaxResult.success(list);
    }

    /**
     * 导出行政区划列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:export')")
    @Log(title = "行政区划", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CmsArea cmsArea)
    {
        List<CmsArea> list = cmsAreaService.selectCmsAreaList(cmsArea);
        ExcelUtil<CmsArea> util = new ExcelUtil<CmsArea>(CmsArea.class);
        return util.exportExcel(list, "area");
    }

    /**
     * 获取行政区划详细信息
     */
    @PreAuthorize("@ss.hasPermi('area:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsAreaService.selectCmsAreaById(id));
    }

    /**
     * 新增行政区划
     */
    @PreAuthorize("@ss.hasPermi('area:area:add')")
    @Log(title = "行政区划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsArea cmsArea)
    {
        return toAjax(cmsAreaService.insertCmsArea(cmsArea));
    }

    /**
     * 修改行政区划
     */
    @PreAuthorize("@ss.hasPermi('area:area:edit')")
    @Log(title = "行政区划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsArea cmsArea)
    {
        return toAjax(cmsAreaService.updateCmsArea(cmsArea));
    }

    /**
     * 删除行政区划
     */
    @PreAuthorize("@ss.hasPermi('area:area:remove')")
    @Log(title = "行政区划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsAreaService.deleteAreaByIds(ids));
    }
}
